<?php
session_start();
if (!isset($_SESSION['user_id']) || $_SESSION['role'] !== 'user') {
    header("Location: ../login.php");
    exit();
}

require '../config.php';

$user_id = $_SESSION['user_id'];

// 获取用户的通知
$stmt = $conn->prepare("SELECT id, type, content, is_read, created_at FROM notifications WHERE user_id = ? ORDER BY created_at DESC");
$stmt->bind_param("i", $user_id);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($notification_id, $notification_type, $notification_content, $is_read, $created_at);

// 标记通知为已读
$update_stmt = $conn->prepare("UPDATE notifications SET is_read = TRUE WHERE user_id = ?");
$update_stmt->bind_param("i", $user_id);
$update_stmt->execute();
$update_stmt->close();
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>通知 - 论坛</title>
    <link href="https://cdn.jsdelivr.net/npm/tailwindcss@2.2.19/dist/tailwind.min.css" rel="stylesheet">
    <style>
        body {
            background-image: url('https://images.unsplash.com/photo-1506748686214-e9df14d4d9d0');
            background-size: cover;
            background-position: center;
            color: #4c4948;
            font-family: 'Lato', Arial, sans-serif;
            margin: 0;
            display: flex;
            min-height: 100vh;
        }
        .overlay {
            background: rgba(255, 255, 255, 0.8);
            width: 100%;
            display: flex;
        }
        .sidebar {
            background-color: #2d3748;
            color: white;
            width: 250px;
            padding: 20px;
            display: flex;
            flex-direction: column;
            align-items: center;
            border-radius: 15px;
            margin: 20px;
        }
        .sidebar a {
            color: white;
            display: block;
            padding: 10px 20px;
            margin: 10px 0;
            background: #3b4252;
            border-radius: 25px;
            transition: background 0.3s;
        }
        .sidebar a:hover {
            background: #a0aec0;
        }
        .main-content {
            flex: 1;
            padding: 20px;
            display: flex;
            flex-direction: column;
            margin: 20px;
            border-radius: 15px;
            background: #fff;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
        }
        .bg-header {
            background-image: url('https://images.unsplash.com/photo-1542281286-9e0a16bb7366');
            background-size: cover;
            background-position: center;
            height: 200px;
            border-radius: 15px;
            position: relative;
            overflow: hidden;
        }
        .bg-header::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: rgba(0, 0, 0, 0.5);
            z-index: 0;
        }
        .bg-header h2 {
            position: relative;
            z-index: 1;
            color: white;
            text-align: center;
            line-height: 200px;
            font-size: 2rem;
            margin: 0;
        }
        .card {
            background: white;
            border-radius: 15px;
            padding: 20px;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
            margin-top: 20px;
        }
        .notification-list li {
            padding: 20px;
            border-bottom: 1px solid #e2e8f0;
        }
        .notification-list .read {
            background: #e2e8f0;
        }
        .notification-list .unread {
            background: #cbd5e0;
        }
    </style>
</head>
<body>
    <div class="overlay">
        <div class="sidebar">
            <h2 class="text-3xl font-bold mb-8">通知</h2>
            <nav class="w-full">
                <a href="index.php" class="text-lg">我的主页</a>
                <a href="create_post.php" class="text-lg">发布新帖子</a>
                <a href="notifications.php" class="text-lg">通知</a>
                <a href="../logout.php" class="text-lg text-red-500 hover:text-red-700">退出登录</a>
            </nav>
        </div>
        <div class="main-content">
            <div class="bg-header">
                <h2>通知中心</h2>
            </div>
            <div class="card">
                <h3 class="text-2xl font-bold text-gray-800 mb-8">我的通知</h3>
                <ul class="notification-list space-y-8">
                    <?php
                    if ($stmt->num_rows > 0) {
                        while ($stmt->fetch()) {
                            echo "<li class='" . ($is_read ? "read" : "unread") . "'>
                                <div class='notification-content text-xl mb-2'>" . htmlspecialchars($notification_content) . "</div>
                                <div class='notification-meta text-sm text-gray-500'>发布时间: $created_at</div>
                            </li>";
                        }
                    } else {
                        echo "<li class='text-gray-500'>暂无通知</li>";
                    }
                    $stmt->close();
                    $conn->close();
                    ?>
                </ul>
            </div>
        </div>
    </div>
</body>
</html>